using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sample
{
    public partial class CustomerTypeForm : System.Web.UI.Page
    {
        public enum DisplayType
        {
            Edit, Add
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if(GetDisplayType == DisplayType.Add)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                    
                    
                    }
                }            
                if (GetDisplayType == DisplayType.Edit)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                        CustomerType entity = db.CustomerTypes.Single(_ => _.Id == GetEditID);
                        CustomerTypeCustomerTypeNameInput.Text = entity.CustomerTypeName;
                        CustomerTypeIsActiveInput.Checked = entity.IsActive;
                        }
                }
            }
        }
        
        
        
        public DisplayType GetDisplayType
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                    return DisplayType.Edit;
                else
                    return DisplayType.Add;
            }
        }
        private int GetEditID
        {
            get { return int.Parse(Request.QueryString["ID"]); }
        }
        protected void CustomerTypeSaveButton_Click(object sender, EventArgs e)
        {
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                CustomerType entity;
                if (GetDisplayType == DisplayType.Edit)
                    entity = db.CustomerTypes.Single(_ => _.Id == GetEditID);
                else
                {
                    entity = new CustomerType();
                    db.CustomerTypes.InsertOnSubmit(entity);
                }
                entity.CustomerTypeName = CustomerTypeCustomerTypeNameInput.Text;
                entity.IsActive = CustomerTypeIsActiveInput.Checked;
                db.SubmitChanges();
            }
        }
        protected void CustomerTypeCancelButton_Click(object sender, EventArgs e)
        {

        }
    }
}